package com.huawei.hms.framework.common;

import android.util.Log;
import com.huawei.hwid.core.constants.HwAccountConstants;

public class Logger {
    private static final boolean DEBUG = false;
    private static final String TAG = "NetworkKit_Logger";
    private static final String TAG_NETWORKKIT_PRE = "NetworkKit_";

    static class ThrowableWrapper extends Throwable {
        private static final long serialVersionUID = 7129050843360571879L;
        private String message;
        private Throwable ownerThrowable;
        private Throwable thisCause;

        private ThrowableWrapper(Throwable th) {
            this.ownerThrowable = th;
        }

        private void setCause(Throwable th) {
            this.thisCause = th;
        }

        public Throwable getCause() {
            return this.thisCause == this ? null : this.thisCause;
        }

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String str) {
            this.message = str;
        }

        public String toString() {
            if (this.ownerThrowable == null) {
                return "";
            }
            String name = this.ownerThrowable.getClass().getName();
            if (this.message == null) {
                return name;
            }
            name = name + ": ";
            return this.message.startsWith(name) ? this.message : name + this.message;
        }
    }

    private static String complexTag(String str) {
        return TAG_NETWORKKIT_PRE + str;
    }

    public static void d(String str, Object obj) {
    }

    public static void d(String str, String str2, Object... objArr) {
    }

    public static void e(String str, Object obj) {
        Log.e(complexTag(str), obj == null ? HwAccountConstants.NULL : obj.toString());
    }

    public static void e(String str, String str2, Throwable th) {
        Log.e(complexTag(str), str2, getNewThrowable(th));
    }

    public static void e(String str, String str2, Object... objArr) {
        if (str2 == null) {
            Log.w(TAG, "format is null, not log");
            return;
        }
        try {
            Log.e(complexTag(str), StringUtils.format(str2, objArr));
        } catch (Throwable e) {
            w(TAG, "log format error" + str2, e);
        }
    }

    private static Throwable getNewThrowable(Throwable th) {
        if (th == null) {
            return null;
        }
        Throwable throwableWrapper = new ThrowableWrapper(th);
        throwableWrapper.setStackTrace(th.getStackTrace());
        throwableWrapper.setMessage(StringUtils.anonymizeMessage(th.getMessage()));
        Throwable cause = th.getCause();
        ThrowableWrapper throwableWrapper2 = throwableWrapper;
        while (cause != null) {
            Throwable throwableWrapper3 = new ThrowableWrapper(cause);
            throwableWrapper3.setStackTrace(cause.getStackTrace());
            throwableWrapper3.setMessage(StringUtils.anonymizeMessage(cause.getMessage()));
            throwableWrapper2.setCause(throwableWrapper3);
            cause = cause.getCause();
            Throwable th2 = throwableWrapper3;
        }
        return throwableWrapper;
    }

    public static void i(String str, Object obj) {
        Log.i(complexTag(str), obj == null ? HwAccountConstants.NULL : obj.toString());
    }

    public static void i(String str, String str2, Object... objArr) {
        if (str2 == null) {
            Log.w(TAG, "format is null, not log");
            return;
        }
        try {
            Log.i(complexTag(str), StringUtils.format(str2, objArr));
        } catch (Throwable e) {
            w(TAG, "log format error" + str2, e);
        }
    }

    public static void v(String str, Object obj) {
    }

    public static void v(String str, String str2, Object... objArr) {
    }

    public static void w(String str, Object obj) {
        Log.w(complexTag(str), obj == null ? HwAccountConstants.NULL : obj.toString());
    }

    public static void w(String str, String str2, Throwable th) {
        Log.w(complexTag(str), str2, getNewThrowable(th));
    }

    public static void w(String str, String str2, Object... objArr) {
        if (str2 == null) {
            Log.w(TAG, "format is null, not log");
            return;
        }
        try {
            Log.w(complexTag(str), StringUtils.format(str2, objArr));
        } catch (Throwable e) {
            w(TAG, "log format error" + str2, e);
        }
    }
}
